package com.lw.spring.service;

import com.lw.spring.service.impl.UserServiceImpl;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/4 15:22
 */
public class BeanFactory {

    public static UserService getUserService(){
        UserService userService=new UserServiceImpl();
        UserService o = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                TransactionUtils.start();
                method.invoke(userService,objects);
                TransactionUtils.commit();
                return null;
            }
        });
        return o;
    }
}
